﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Albums;

    public class SharedAlbumEventArgs : EventArgs
    {
        private int albumCount;
        public int AlbumCount { get { return albumCount; } }
        public SharedAlbumEventArgs(int count)
        {
            albumCount = count;
        }

    }

    public partial class SharedAlbum : SharedUserControl
    {
        private string viewAlbumUrl = string.Empty;
        private string albumIdQueryString = string.Empty;
        //private string uploadUrl = string.Empty;
        //private string uploadLinkText = string.Empty;
        private string extraQueryString = string.Empty;

        public string ViewAlbumUrl { get { return viewAlbumUrl; } set { viewAlbumUrl = value; } }
        public string AlbumIdQueryStringField { get { return albumIdQueryString; } set { albumIdQueryString = value; } }
        //public string UploadUrl { get { return uploadUrl; } set { uploadUrl = value; } }
        //public string UploadLinkText { get { return uploadLinkText; } set { uploadLinkText = value; } }
        public string ExtraQueryString { get { return extraQueryString; } set { extraQueryString = value; } }
        public event EventHandler<SharedAlbumEventArgs> AlbumListShown;


        protected void Page_Init(object sender, EventArgs e)
        {
            ListView1.DataBound += new EventHandler(ListView1_DataBound);
            ListView1.ItemDataBound += new EventHandler<ListViewItemEventArgs>(ListView1_ItemDataBound);
        }

        void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            AlbumsWithCoverImage data = (AlbumsWithCoverImage)((ListViewDataItem)e.Item).DataItem;
            //HyperLink lnkUpload = (HyperLink)e.Item.FindControl("lnkUpload");
            //if (uploadLinkText != string.Empty)
            //{
            //    lnkUpload.Text = uploadLinkText;
            //}
            //lnkUpload.NavigateUrl = string.Format("{0}?{1}={2}&{3}", uploadUrl, albumIdQueryString, data.AlbumId, extraQueryString);

            HyperLink lnkAlbum = (HyperLink)e.Item.FindControl("lnkAlbum");
            lnkAlbum.NavigateUrl = string.Format("{0}?{1}={2}&{3}", viewAlbumUrl, albumIdQueryString, data.AlbumId, extraQueryString);

            Image imgCover = (Image)e.Item.FindControl("imgCover");
            if (data.FileName == string.Empty || data.FileName == null)
            {
                imgCover.Visible = false;
            }
            else
            {
                imgCover.ImageUrl = string.Format("~/Content/Albums/{0}/thumb/{1}", data.AlbumId, data.FileName);
            }
        }

        void ListView1_DataBound(object sender, EventArgs e)
        {
            
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            Panel1.CssClass = CssClass;
            List<AlbumsWithCoverImage> list = AlbumOperations.GetAlbums(EntityType, ContextId);
            ListView1.DataSource = list;
            ListView1.DataBind();
            AlbumListShown(this, new SharedAlbumEventArgs(list.Count));
        }
    }
}